<?php

/**
 * Created by PhpStorm.
 * User:money
 * Date: 17-6-21
 * Time: 下午1:44
 */
namespace V1;
class OrderList extends \ResourceClass
{
    public $isActive = true;
    public $remark = '订单列表';

    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require uid ';
        }
        $where = "";
        if (isset($req['buyer_uid'])) {
            $where = "buyer_uid = {$req['uid']}";
        }
        if (isset($req['seller_uid'])) {
            $where = strlen($where) ? $where . " AND " :  "seller_uid = {$req['uid']}";
        }
        if (isset($req['article_id'])) {
            $where = "article_id = {$req['article_id']}";
        }
        if (isset($req['pay_status'])){
            $where = "seller_uid = {$req['uid']}" . " AND " . "pay_status = {$req['pay_status']}";
        }
        $result = array();
        $order_ids = array();
        $seller_uid = array();
        try {
            $order_list = new \Orm\Order\OrderList();
            $order_status_list = new \Orm\Order\OrderClass();
            $order_list['o'] = isset($req['o']) ? $req['o'] : 20 ;
            $order_list['l'] = isset($req['l']) ? $req['l'] : 0;
            $order_list['key_by'] = 'id';
            $order_list['where'] = $where;
            $order_list->load();
            foreach ($order_list['lists'] as $id=>$list) {
                $result[$id]['id'] = (string)$list['id'];
                $result[$id]['order_code'] = (string)$list['order_code'];
                $result[$id]['article_id'] = (string)$list['article_id'];
                $result[$id]['seller_uid'] = (string)$list['seller_uid'];
                $result[$id]['buyer_uid'] = (string)$list['buyer_uid'];
                $result[$id]['contact'] = (string)$list['contact'];
                $result[$id]['tel'] = (string)$list['tel'];
                $result[$id]['adds'] = (string)$list['adds'];
                $result[$id]['total_price'] = (string)$list['total_price'];
                $result[$id]['remark'] = (string)$list['remark'];
                $result[$id]['pay_status'] = $order_status_list->getPayById($list['pay_status']);//(string)$list['pay_status'];
                $result[$id]['pay_datetime'] = (string)$list['pay_datetime'];
                $result[$id]['appointment_time'] = (string)$list['appointment_time'];
                $result[$id]['logistics_name'] = (string)$list['logistics_name'];
                $result[$id]['logistics_numbers'] = (string)$list['logistics_numbers'];
                $result[$id]['order_status'] = $order_status_list->getOrderById($list['order_status']);//(string)$list['order_status'];
                $result[$id]['insert_time'] = (string)$list['insert_time'];
                $result[$id]['detail_list'] = array();
                array_push($order_ids, $list['id']);
                array_push($seller_uid,$list['seller_uid']);
            }
            $user_list = $order_list->userDetail($seller_uid);
            $user_list_tmp = array();
            if(count($user_list)){
                foreach ($user_list as $item) {
                    $user_list_tmp[$item['id']] = array(
                        'id'=> $item['id'],
                        'open_id' => $item['open_id'],
                        'nickname' => $item['nickname'],
                        'headimgurl' => $item['headimgurl'],
                    );
                }
            }
            $result['user_list'] = $user_list_tmp;
            $detail_list = $order_list->completeDetail($order_ids);
            if(count($detail_list)) {
                foreach ($detail_list as $item) {
                    $result[$item['order_id']]['detail_list'] = array(
                        'id'=> $item['id'],
                        'order_id' => $item['order_id'],
                        'product_id' => $item['product_id'],
                        'unit_price' => $item['unit_price'],
                        'amount' => $item['amount'],
                        'total_price' => $item['total_price'],
                    );
                }
            }

        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    public function post(array $req, array $post, &$result)
    {
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data, $data);
        if (!isset($data['logistics_name'])) {
            return '物流名称不能为空';
        }
        if (!isset($data['logistics_numbers'])) {
            return '物流单号不能为空';
        }
        try {
            $user_class = new \Orm\Order\OrderClass();
            $user_class->loadById($data['id']);
            $user_class['logistics_name'] = $data['logistics_name'];
            $user_class['logistics_numbers'] = $data['logistics_numbers'];
            $user_class['order_status'] = '1';
            $user_class->update();

            $result = array(
                'logistics_name' => $user_class['logistics_name'],
                'logistics_numbers' => $user_class['logistics_numbers'],
                'order_status' => $user_class['order_status'],
            );
        }catch (\Exception $e ) {
            return $e->getMessage();
        }
        return '';
    }

    public function put(array $req, array $post, &$result)
    {
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data, $data);
        try {
            $user_class = new \Orm\Order\OrderClass();
            $user_class->loadById($data['id']);
            $user_class['order_status'] = '2';
            $user_class->update();

            $result = array(
                'order_status' => $user_class['order_status'],
            );
        }catch (\Exception $e ) {
            return $e->getMessage();
        }
        return '';
    }
}